function LRUCache(limit) {
    this._head = null;
    this._end = null;
    this._limit = limit;
    this._hashMap = {};
}

function Node(key, value) {
    this.key = key;
    this.value = value;
    this.pre = null;
    this.next = null;
}

LRUCache.prototype.get = function(key) {
    var node = this._hashMap[key];
    if(!node) {
        return null;
    }
    this._refreshNode(node);
    return node.value;
};

LRUCache.prototype.put = function(key, value) {
    var node = this._hashMap[key];
    if(!node) {
        if(Object.keys(this._hashMap).length >= this._limit) {
            var oldKey = this._removeNode(this._head);
            delete this._hashMap[oldKey];
        }
        
        node = new Node(key, value);
        this._addNode(node);
        this._hashMap[key] = node;
    } else {
        node.value = value;
        this._refreshNode(node);
    }
};

LRUCache.prototype.remove = function(key) {    
    var node = this._hashMap[key];
    if(!node) {
        return;
    }
    this._removeNode(node);
    delete this._hashMap[key];
};

LRUCache.prototype._refreshNode = function(node) {
    if(node == this._end) {
        return;
    }
    this._removeNode(node);
    this._addNode(node);
};

LRUCache.prototype._removeNode = function(node) {
    if(node == this._head && node == this._end){
        this._head = null;
        this._end = null;
    }else if(node == this._end){
        //移除尾节点
        this._end = this._end.pre;
        this._end.next = null;
    }else if(node == this._head){
        //移除头节点
        this._head = this._head.next;
        this._head.pre = null;
    }else {
        //移除中间节点
        node.pre.next = node.next;
        node.next.pre = node.pre;
    }
    return node.key;
};

LRUCache.prototype._addNode = function(node) {
    if(this._end != null) {
        this._end.next = node;
        node.pre = this._end;
        node.next = null;
    }
    this._end = node;
    if(this._head == null){
        this._head = node;
    }
};

var lruCache = new LRUCache(5);
lruCache.put("001", "用户1信息");
lruCache.put("002", "用户2信息");
lruCache.put("003", "用户3信息");
lruCache.put("004", "用户4信息");
lruCache.put("005", "用户5信息");
lruCache.get("002");
lruCache.put("004", "用户4信息更新");
lruCache.put("006", "用户6信息");
console.log(lruCache.get("001"));
console.log(lruCache.get("006"));